package com.zy.service.impl;

import com.zy.dao.DepartmentDao;
import com.zy.dao.EmployeeDao;
import com.zy.dao.RoleDao;
import com.zy.dao.SalaryDao;
import com.zy.dao.impl.DepartmentDaoImpl;
import com.zy.dao.impl.EmployeeDaoImpl;
import com.zy.dao.impl.RoleDaoImpl;
import com.zy.dao.impl.SalaryDaoImpl;
import com.zy.pojo.Department;
import com.zy.pojo.Employee;
import com.zy.pojo.Role;
import com.zy.service.EmployeeService;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2021/5/31
 * Time: 下午 16:29
 * Description: 员工服务层接口实现类
 */
public class EmployeeServiceImpl implements EmployeeService {
    private EmployeeDao employeeDao = new EmployeeDaoImpl();
    private DepartmentDao departmentDao = new DepartmentDaoImpl();
    private RoleDao roleDao = new RoleDaoImpl();
    private SalaryDao salaryDao = new SalaryDaoImpl();

    @Override
    public Boolean insertEmployee(Employee employee) {
        Integer result = employeeDao.insertEmployee(employee);
        if (result > 0) {
            roleDao.insertEmpRole(employee.getRole());
        }
        return result > 0;
    }

    @Override
    public Boolean deleteEmployeeById(Integer id) {
        boolean result = employeeDao.deleteEmployeeById(id) > 0;
        if (result) {
            salaryDao.deleteSalaryByEmpId(id);
        }
        return result;
    }

    @Override
    public Boolean updateEmployee(Employee employee) {
        if (roleDao.getRoleByEmpId(employee.getId()) != null) {
            roleDao.updateRoleByEmpId(employee.getRole());
        } else {
            employee.getRole().setEmpId(employee.getId());
            roleDao.insertEmpRole(employee.getRole());
        }
        return employeeDao.updateEmployee(employee) > 0;
    }

    @Override
    public List<Employee> getEmployeeListPage(Integer page, Integer limit, Employee employee) {
        List<Employee> employeeList = employeeDao.getEmployeeList((page - 1) * limit, limit, employee);
        employeeList.forEach(temp -> {
            Department departmentById = departmentDao.getDepartmentById(temp.getDepartmentId());
            Role role = roleDao.getRoleByEmpId(temp.getId());
            temp.setRole(role);
            temp.setDepartment(departmentById);
        });
        return employeeList;
    }

    @Override
    public Employee getEmployeeById(Integer id) {
        return employeeDao.getEmployeeById(id);
    }

    @Override
    public Employee getEmployeeByNameAndPassword(String name, String password) {
        // 首先查询还用户是否存在
        Integer count = employeeDao.getEmployeeCountByName(name);
        if (count <= 0) {
            return null;
        }
        // 然后根据用户名和密码查询数据库
        Employee employee = employeeDao.getEmployeeByNameAndPassword(name, password);
        if (employee == null) {
            //如果查询出来为空 说明用户名或密码错误，那么新建一个对象，设置name为用户名或密码错误
            Employee result = new Employee();
            result.setName("用户名或密码错误");
        }
        Role role = roleDao.getRoleByEmpId(employee.getId());
        if (role == null) {
            role = new Role();
            role.setEmpId(employee.getId());
            role.setRoleName("普通员工");
        }
        employee.setRole(role);
        return employee;
    }

    @Override
    public Integer getEmployeeCount(Employee employee) {
        return employeeDao.getEmployeeCount(employee);
    }
}